package com.yidu.service.impl;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.yidu.comment.Tools;
import com.yidu.dao.SnBomMapper;
import com.yidu.dao.SnComInDetailMapper;
import com.yidu.dao.SnComInventoryMapper;
import com.yidu.dao.SnCommodityMapper;
import com.yidu.model.SnBom;
import com.yidu.model.SnComInDetail;
import com.yidu.model.SnComInventory;
import com.yidu.model.SnCommodity;
import com.yidu.model.SnProDetail;
import com.yidu.model.SnUser;
import com.yidu.service.SnCommodityService;
/**
 * 商品表实现类
 * @author chensituo
 * @date 2018年11月27日 下午4:09:42
 */
@Service
public class SnCommodityServiceImpl implements SnCommodityService{
	/**
	 * 注入商品mapper
	 */
	@Resource
	SnCommodityMapper commodityMapper;
	/**
	 * 注入商品库存mapper
	 */
	@Resource
	SnComInventoryMapper comInventoryMapper;
	/**
	 * 注入商品库存记录
	 */
	@Resource
	SnComInDetailMapper comInDetailMapper;
	
	/**
	 * 注入物料清单mapper
	 */
	@Resource
	SnBomMapper bomMaapper;
	
	@Override
	public List<SnProDetail> findAll(Map<String, Object> maps) {
		//查询所有的方法
		return commodityMapper.findAll(maps);
	}

	@Override
	public int findCount(Map<String, Object> maps) {
		//调用查询行数的方法
		return commodityMapper.findCount(maps);
	}

	@Override
	public int addOrUpdate(SnCommodity commodity) {
		int rows=0;
		//如果商品id不为空
		if(commodity.getComId()!=null && !"".equals(commodity.getComId())) {
			
			//创建一个map
			Map<String, Object> map=new HashMap<>();
			//将商品id包装到map
			map.put("comId", commodity.getComId());
			//将分公司id包装到map
			map.put("braId", "123");
			//调用商品库存mapper根据商品id 和总公司查询库存的方法 得到商品库存model
			SnComInventory comInventory=comInventoryMapper.findComBranId(map);
			//将成本价赋值
			comInventory.setCiSales(commodity.getCiSales());
			//将商品单位赋值
			comInventory.setCiUnit(commodity.getComUnit());
			//调用商品库存mapper修改的方法
			comInventoryMapper.updateByPrimaryKeySelective(comInventory);
			//调用商品mapper修改的方法
			commodityMapper.updateByPrimaryKeySelective(commodity);
			
		}else {
			//增加
			//将商品model的商品id赋值
			commodity.setComId(Tools.getDateOrderNo());
			//将商品model的操作时间赋值
			commodity.setOptime(Tools.getCurDateTime());
			//将商品model的是否有效赋值
			commodity.setIsva(1);
			//将商品model的排序赋值
			commodity.setSort(Integer.valueOf(Tools.getTimeStamp()));
			//调用商品mapper增加商品的方法
			rows=commodityMapper.insertSelective(commodity);
			//创建商品库存对象
			SnComInventory comInventory=new SnComInventory();
			//将商品库存model的库存id赋值
			comInventory.setCiId(Tools.getDateOrderNo());
			//将商品库存model的商品id赋值
			comInventory.setComId(commodity.getComId());
			//将商品库存model的分公司id赋值
			comInventory.setBraId("123");
			//将商品库存model的数量赋值
			comInventory.setCiNumber(new BigDecimal(0));
			//将商品库存model的操作时间赋值
			comInventory.setOptime(Tools.getCurDateTime());
			//将商品库存model的商品单位赋值
			comInventory.setCiUnit(commodity.getComUnit());
			//将商品库存model的是否有效赋值
			comInventory.setIsva(1);
			//将商品库存model的排序赋值
			comInventory.setSort(Integer.valueOf(Tools.getTimeStamp()));
			//将商品库存model的销售价赋值
			comInventory.setCiSales(commodity.getCiSales());
			//将商品库存model的成本价赋值
			comInventory.setCiCost(new BigDecimal(commodity.getCiCost()));
			//调用商品库存mapper增加库存的方法
			comInventoryMapper.insertSelective(comInventory);
			
			//创建商品库存对象
			SnComInDetail comInDetail=new SnComInDetail();
			//将商品库存记录id赋值
			comInDetail.setCidId(Tools.getDateOrderNo());
			//将商品库存记录库存id赋值
			comInDetail.setCiId(comInventory.getCiId());
			//将商品库存记录支出记录赋值
			comInDetail.setCidExplain("增加商品"+commodity.getComName()+"库存0");
			//将商品库存记录是否有效赋值
			comInDetail.setIsva(1);
			//将商品库存记录操作时间赋值
			comInDetail.setOptime(Tools.getCurDateTime());
			//将商品库存记录排序赋值
			comInDetail.setSort(Integer.valueOf(Tools.getTimeStamp()));
			//调用商品库存详情mapper增加库存记录的方法
			comInDetailMapper.insertSelective(comInDetail);
		}
		return rows;
	}

	@Override
	public SnCommodity findById(String comId) {
		//调用根据id查询的方法
		return commodityMapper.findByIdAll(comId);
	}

	@Override
	public int delete(String comId,SnUser user) {
		//创建mp
		Map<String,Object> map=new HashMap<>();
		//将商品id封装到map
		map.put("comId", comId);
		//将分公司id封装到map
		map.put("braId", user.getBraId());
		//根据商品id和分公司id查询该商品的库存 得到商品库存model
		SnComInventory comInventory=comInventoryMapper.findComBranId(map);
		//将是否有效改为无效
		comInventory.setIsva(0);
		//调用商品库存mapper的修改方法
		comInventoryMapper.updateByPrimaryKeySelective(comInventory);
		 
		//根据库存id查询商品库存明细mapper 得到库存明细集合
		List<SnComInDetail> listdetail=comInDetailMapper.findCiId(comInventory.getCiId());
		//遍历库存明细集合
		for (SnComInDetail snComInDetail : listdetail) {
			//将是否有效改为无效
			snComInDetail.setIsva(0);
			//调用库存记录详情mapper修改库存记录的方法
			comInDetailMapper.updateByPrimaryKeySelective(snComInDetail);
		}
		//根据商品id查询物料清单mapper 得到物料清单集合
		List<SnBom> listBom=bomMaapper.selectComId(comId);
		//如果物料清单集合不为空
		if(!listBom.isEmpty()) {
			//遍历物料清单集合
			for (SnBom snBom : listBom) {
				//将是否有效改为无效
				snBom.setIsva(0);
				//调用物料清单mapper修改的方法
				bomMaapper.updateByPrimaryKeySelective(snBom);
			}
		}
		
		
		//根据商品id查询商品mapper 得到商品model
		SnCommodity commodity=commodityMapper.selectByPrimaryKey(comId);
		//将是否有效改为无效
		commodity.setIsva(0);
		//调用商品mapper修改的方法
		return commodityMapper.updateByPrimaryKeySelective(commodity);
	}

	@Override
	public List<SnCommodity> query() {
		return commodityMapper.query();
	}

	@Override
	public List<SnProDetail> findAll2(Map<String, Object> maps) {
		return commodityMapper.findAll2(maps);
	}

	@Override
	public int findCount2(Map<String, Object> maps) {
		return commodityMapper.findCount2(maps);
	}
}
